package com.yz.xyt.controller;

import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.Dorm;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.service.IDormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 宿舍controller
 */
@RestController
@RequestMapping("/dorm")
public class DormController {

    @Autowired
    IDormService dormService;

    /**
     * 添加宿舍
     */
    @PostMapping("/addDorm")
    public ResponseResult addDorm(Dorm dorm){

        ResponseResult result = new ResponseResult();

        //获取传递的参数
        if(Objects.isNull(dorm)){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("参数错误");
        }else{
            int i = dormService.addDorm(dorm);
            if(i != 0){
                result.setCode(HttpStatus.SUCCESS);
                result.setMsg("宿舍信息添加成功");
            }else {
                result.setCode(HttpStatus.ERROR);
                result.setMsg("宿舍信息添加失败");
            }
        }
        return result;
    }

    /**
     * 同步增量
     * @param list
     * @return
     */
    @PostMapping("/syncAdd")
    public ResponseResult syncAddDorm(@RequestBody List<Dorm> list){

        ResponseResult result = new ResponseResult();

        //获取传递的参数
        if(Objects.isNull(list)){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("参数错误");
        }else{
            dormService.syncAddDorm(list);
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("宿舍信息增量同步成功");
        }

        return result;
    }

    /**
     * 同步更新
     * @param list
     * @return
     */
    @PostMapping("/syncUpdate")
    public ResponseResult syncUpdateDorm(@RequestBody List<Dorm> list){

        ResponseResult result = new ResponseResult();

        //获取传递的参数
        if(Objects.isNull(list)){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("参数错误");
        }else{
            dormService.syncUpdateDorm(list);
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("宿舍信息更新同步成功");
        }

        return result;
    }
    /**
     * 同步删除
     * @param list
     * @return
     */
    @PostMapping("/syncDelete")
    public ResponseResult syncDeleteDorm(@RequestBody List<Dorm> list){

        ResponseResult result = new ResponseResult();

        //获取传递的参数
        if(Objects.isNull(list)){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("参数错误");
        }else{
            dormService.syncDeleteDorm(list);
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("宿舍信息删除同步成功");
        }

        return result;
    }
}
